package com.h2cloud.JUCTest.reentrantLock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Package: com.h2cloud.JUCTest.reentrantLock
 * @ClassName: myTest
 * @Author: h2cloud
 * @CreateTime: 2022/9/18 10:57 上午
 * @Description:
 */
@Slf4j
public class myTest {
	public static void main(String[] args) {
		// LockSupport.unpark(Thread.currentThread());
		// LockSupport.unpark(Thread.currentThread());
		// LockSupport.unpark(Thread.currentThread());
		// LockSupport.park(Thread.currentThread());
		// System.out.println("he 1");
		// LockSupport.park(Thread.currentThread());
		// System.out.println("he 2");

		Thread t0 = new Thread(new Runnable() {
			@Override
			public void run() {
				int i = 0;
				while (true) {
					i++;
					if (i == 10) {
						LockSupport.park();
					}
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("run");
				}
			}
		}, "t0");
		t0.start();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		t0.interrupt();

	}

	public static void test1() {
		ReentrantLock lock = new ReentrantLock();
		new Thread(() -> {
			lock.lock();
			try {
				Thread.sleep(7000);
				System.out.println("t1 end");
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}, "t1").start();

		for (int i = 0; i < 100; i++) {

			int value = i;
			new Thread(() -> {
				if (value == 0) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				lock.lock();
				try {
					System.out.println("t1: " + value + ", end");
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}, "t1").start();
			// try {
			// 	Thread.sleep(50);
			// } catch (InterruptedException e) {
			// 	e.printStackTrace();
			// }
		}
	}
}
